package com.example.zhangruoyu_zoe.activities



import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.example.zhangruoyu_zoe.R
import com.example.zhangruoyu_zoe.data.Student

class AddStudentActivity : AppCompatActivity() {

    private lateinit var idEditText: EditText
    private lateinit var nameEditText: EditText
    private lateinit var genderEditText: EditText
    private lateinit var classNameEditText: EditText
    private lateinit var addButton: Button


    @SuppressLint("MissingInflatedId")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_add_student)

        idEditText = findViewById(R.id.idEditText)
        nameEditText = findViewById(R.id.nameEditText)
        genderEditText = findViewById(R.id.genderEditText)
        classNameEditText = findViewById(R.id.classNameEditText)
        addButton = findViewById(R.id.addButton)
        val cancel_button=findViewById<Button>(R.id.cancel_button)
        cancel_button.setOnClickListener{
            val intent=Intent()
            finish()
        }
        addButton.setOnClickListener {
            val id = idEditText.text.toString()
            val name = nameEditText.text.toString()
            val gender = genderEditText.text.toString()
            val className = classNameEditText.text.toString()

            if (id.isNotEmpty() && name.isNotEmpty() && gender.isNotEmpty() && className.isNotEmpty()) {
                val student = Student(id, name, gender, className)
                val intent = Intent()
                intent.putExtra("new_student", student)
                setResult(RESULT_OK, intent)
                finish()
            } else {
                Toast.makeText(this, "请填写所有字段", Toast.LENGTH_SHORT).show()
            }
        }
    }
}